//# sourceURL=http://uMapApp/UI/Panel/PanelBlock/ListBlock
define([
    "mhjs/mhjs"
    , "./PanelBlock"
    , "../Item/ListItem"
    , "./Filter"
    , "./Sorter"
], function (
    mhjs,
    PanelBlock,
    ListItem,
    Filter,
    Sorter
) {

    function ListBlock(option) {

        var _this = this;
        _this.isOpened = false;

        _this._list = [];

        PanelBlock.call(_this, option);
        _this.addClass("listBlock");

        _this.ui = _this.ui || {};

        _this.ui.filterBlock = new mhjs.Div({"class": "filterBlock"});

        _this.ui.wrap
            .before(_this.ui.filterBlock.dom);

    }

    var _class = ListBlock;
    mhjs.Inherit(_class, PanelBlock);


    Object.defineProperties(_class.prototype, {
        "list"  : {
            set: function (list) {
                var _this = this;
                _this.clear();
                $.each(list, function (i, item) {
                    _this.add(item);
                });
            },
            get: function () {
                return this._list;
            }
        },
        "add"   : {
            value: function (item) {
                var _this = this;
                _this.list.push(item);
                _this.ui.wrap.append(item);
            }
        },
        "clear" : {
            value: function () {
                var _this = this;
                _this._list = [];
                _this.ui.wrap.empty();
            }
        },
        "filter": {
            set: function (filter) {
                var _this = this;
                filter.list = _this.list;
                _this._filter = new Filter(filter);

                _this.addClass("hasFilter");
                _this.ui.filterBlock.append(_this.filter);

                _this.filter.on("update", function () {
                    $.each(_this.filter.filter.failIndexs, function (i, item) {
                        _this.list[item].hide();
                    });
                    $.each(_this.filter.filter.okIndexs, function (i, item) {
                        _this.list[item].show();
                    });
                });

            },
            get: function () {
                return this._filter;
            }
        },
        "sorter": {
            set: function (sorter) {
                var _this = this;
                sorter.list = _this.list;
                _this._sorter = new Sorter(sorter);

                _this.addClass("hasFilter");
                _this.ui.filterBlock.append(_this.sorter);

                _this.sorter.on("update", function () {
                    $.each(_this.sorter.sorter.resultIndexs, function (i, index) {
                        _this.list[index].dom.detach().appendTo(_this.ui.wrap.dom);
                    });
                });

            },
            get: function () {
                return this._sorter;
            }
        }
    });

    return _class;

});